<?php
namespace Home\Controller;
use Think\Controller;
class QuestionController extends CommonController {
    public function index()
    {
        $id = I('get.id');

        $sql = "select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$id}'";

        $question = M()->query($sql);

        // var_dump($question);exit;

        $question = current($question);

        $this->autoAdd('question','view_num','+1',$id);

        $this->assign('question',$question);

        $sql = "select t1.*,t2.nickname,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$id}'";

        $answer = M()->query($sql);

        $this->assign('title',$question['title']);

        $this->assign('answer',$answer);

        $sql = "select t1.*,t2.nickname,t2.face from store t1 inner join user t2 on t1.uid=t2.id where t1.uid=t2.id and t1.qid='{$id}'";

        $use = M()->query($sql);

        // var_dump($use);exit;

        $this->assign('use',$use);

        // var_dump($use);exit;

        $this->getTopView();

        $this->getTopAnswer();

        $this->display();
    }
    public function add(){
    	$question = D('verify')->getOneVerify();

    	$this->assign("question",$question);

    	$cates = D('category')->getCate();

    	$this->assign("cates",$cates);

        $this->display();
    }

    public function checkquestion()
    {	
    	$verify = I('post.verify');

    	$userinfo = M('user')->where(array('id'=>$_SESSION['id']))->find();

    	if ($verify != $_SESSION['answer']) {

    		$arr = array(
    				'error'=>1,
    				'info'=>'人类验证失败'

    			);
    		echo json_encode($arr);
    		exit;
    	}

    	$kiss = I('post.kiss_num');

        if (!$_SESSION['id']) {
            $arr = array(
                    'error'=>1,
                    'info'=>'请先登录你的账号'

                );
            echo json_encode($arr);
            exit;
        }

    	if ($kiss>$userinfo['kiss']) {
    		$arr = array(
    				'error'=>1,
    				'info'=>'飞吻不够，发表失败'

    			);
    		echo json_encode($arr);
    		exit;
    	}

		$data = I('post.');

        $data['uid'] = $_SESSION['id'];

        $data['create_time'] = time();

		$id = M("question")->add($data);

		$res1 = M("user")->where(array('id'=>$_SESSION['id']))->save($data);

        $num = I('post.kiss_num');

        $this->autoAdd("user","kiss","-$num",$_SESSION['id']);

		if ($id){
    		$arr = array(
    				'error'=>0,
    				'info'=>'发表成功',
                    'url'=>U('home/question/index',array('id'=>$id))
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    				'error'=>1,
    				'info'=>'发表失败'

    			);
    	echo json_encode($arr);
    	exit;

    	}
    }

    public function uploadImages()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','gif');// 设置附件上传类型
        $upload->rootPath  =     './Articleimg/Questionimg/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {
            echo $upload->getError();
        }else{
            $info = current($info);
            $path = __ROOT__."/Articleimg/Questionimg/".$info['savepath'].$info['savename'];
            
            $res = array(
                'code'=>0,
                'msg'=>'成功',
                'data'=>array(
                    'src'=>$path,
                    'title'=>'标题',
                    )
                );

            echo json_encode($res);
         }
                    
     }

     public function answerimg()
     {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','gif');// 设置附件上传类型
        $upload->rootPath  =     './Articleimg/Answerimg/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {
            echo $upload->getError();
        }else{
            $info = current($info);
            $path = __ROOT__."/Articleimg/Answerimg/".$info['savepath'].$info['savename'];
            
            $res = array(
                'code'=>0,
                'msg'=>'成功',
                'data'=>array(
                    'src'=>$path,
                    'title'=>'标题',
                    )
                );

            echo json_encode($res);
         }
     }

     public function answer()
     {
        $data = I('post.');

        $data['uid'] = $_SESSION['id'];

        $data['create_time'] = time();

        $id = M("answer")->add($data);

        if ($id){
            $arr = array(
                    $this->autoAdd('question','answer_num','+1',I('post.qid')),
                    $this->autoAdd('user','answer_num','+1',$_SESSION['id']),
                    'error'=>0,
                    'info'=>'回复成功',
                    'url'=>U('home/question/index',array('id'=>$data['qid']))
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                    'error'=>1,
                    'info'=>'回复失败'

                );
        echo json_encode($arr);
        exit;

        }

     }

     public function edit()
     {
        $id = I('get.id');

        $question = M('question')->find($id);

        $this->assign('question',$question);

        $verify = D('verify')->getOneVerify();

        $cate = D('category')->getCate();

        $this->assign('cate',$cate);

        $this->assign("verify",$verify);

        $this->display();

     }

     public function editData()
     {

        $User = M("question");

        $data = I('post.');

        $res = M("question")->where(array('id'=>I('post.id')))->save($data);

        $id = I('post.id');

        if ($res){
            $arr = array(
                    'error'=>0,
                    'info'=>'修改成功',
                    'url'=>U('home/question/index',array('id'=>$id))
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                    'error'=>1,
                    'info'=>'修改失败'

                );
        echo json_encode($arr);
        exit;

        }
     }

     public function editimg()
     {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','gif');// 设置附件上传类型
        $upload->rootPath  =     './Articleimg/Editimg/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {
            echo $upload->getError();
        }else{
            $info = current($info);
            $path = __ROOT__."/Articleimg/Editimg/".$info['savepath'].$info['savename'];
            
            $res = array(
                'code'=>0,
                'msg'=>'成功',
                'data'=>array(
                    'src'=>$path,
                    'title'=>'标题',
                    )
                );

            echo json_encode($res);
         }
     }

     public function collect()
     {
        $uid = $_SESSION['id'];

        $qid = $_POST['qid'];

        if (M('store')->where(array('uid'=>$uid,'qid'=>$qid))->find()){
            
            M('store')->where(array('uid'=>$uid,'qid'=>$qid))->delete();

            $arr = array(
                    'error'=>1,
                    'info'=>'取消收藏',
                    'url'=>U('home/question/index',array('id'=>$qid))
                );
            echo json_encode($arr);
            exit;
        }else{
            $data['uid'] = $_SESSION['id'];

            $data['qid'] = $_POST['qid'];

            $data['create_time'] = time();

            $collect = M("store")->add($data);

            $arr = array(
                    'error'=>0,
                    'info'=>'收藏成功',
                    'url'=>U('home/question/index',array('id'=>$data['qid']))
                );
            echo json_encode($arr);
            exit;

         }
    }

    public function addZan()
    {
        $id = I('post.aid');

        $uid = $_SESSION['id'];

        if (M('zan')->where(array('uid'=>$uid,'aid'=>$id))->find()){
            
            M('zan')->where(array('uid'=>$uid,'qid'=>$id))->delete();

            $this->autoAdd('answer','zan_num','-1',$id);

            $arr = array(
                    'error'=>1,
                    'info'=>'取消点赞',
                );
            echo json_encode($arr);
            exit;
        }else{
            $data['uid'] = $_SESSION['id'];

            $data['aid'] = $id;

            $data['create_time'] = time();

            $zan = M("zan")->add($data);

            $this->autoAdd('answer','zan_num','+1',$id);

            $arr = array(
                    'error'=>0,
                    'info'=>'点赞成功',
                );
            echo json_encode($arr);
            exit;

         }   

    }

    public function accept()
    {
        $data['status'] = '已采纳';

        $res = M("question")->where(array('id'=>I('post.questionid')))->save($data);

        $data['status'] = 1;

        $res1 = M("answer")->where(array('id'=>I('acceptid')))->save($data);

        $qkiss = M("question")->where(array('id'=>I('post.questionid')))->find();

        $id = I('acceptid');

        $sql = "select * from answer t1 inner join user t2 on t1.uid=t2.id where t1.id={$id}";

        $uid = M()->query($sql);

        $uid = current($uid);

        $this->autoAdd("user","kiss","+".$qkiss['kiss_num'],$uid['id']);

        if ($res1){

            $arr = array(
                    'error'=>0,
                    'info'=>'采纳成功',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                    'error'=>1,
                    'info'=>'采纳失败',
                );
            echo json_encode($arr);
            exit;

         }   

    }

}